package com.andy.wang.controller;

import com.andy.wang.pojo.*;
import com.andy.wang.service.AccroomService;
import com.andy.wang.service.RoomService;
import com.andy.wang.service.RoomchitchatService;
import com.andy.wang.utils.UploadFile;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.UUID;

@RestController
@RequestMapping("/room")
@CrossOrigin
public class roomsController {
    @Autowired
    private RoomService roomService;

    @Autowired
    private AccroomService accroomService;

    @Autowired
    private RoomchitchatService roomchitchatService;

    @GetMapping()
    public Object getRooms(@RequestParam("count") int count, @RequestParam("hobbys") String hobbys, @RequestParam("search") String search) {
        return Result.success(roomService.getRooms(count, hobbys, search));
    }

    @GetMapping("/count")
    public Object getCountRoom() {
        return Result.success(roomService.countRooms());
    }

    // 用户进入房间
    @PostMapping("/addUserRoom")
    public Object addUserRoom(@RequestParam("rID") int rID, @RequestParam("roomInitiator") int roomInitiator, @RequestParam("enteredRoom") int enteredRoom) {
        LambdaQueryWrapper<Accroom> lambdaQueryWrapper = new LambdaQueryWrapper();
        Accroom accroom = new Accroom();
        accroom.setRid(rID);
        accroom.setRoomInitiator(roomInitiator);
        accroom.setEnteredRoom(enteredRoom);
        //        lambdaQueryWrapper
        return Result.success(accroomService.saveOrUpdate(accroom));
    }

    // 获取当前进入房间信息
    @GetMapping("/roomUsers/{rid}")
    public Object getRoomUsers(@PathVariable("rid") int rid) {
        return Result.success(roomService.getRoomUsers(rid));
    }


    @GetMapping("/exitRoom/{rid}/{enteredRoom}")
    public Object getExitRoom(@PathVariable("rid") int rid, @PathVariable("enteredRoom") int enteredRoom) {
        LambdaUpdateWrapper<Accroom> updateWrapper = new LambdaUpdateWrapper<Accroom>();
        updateWrapper.set(Accroom::getIsDeleted, 1)
                .eq(Accroom::getRid, rid)
                .eq(Accroom::getEnteredRoom, enteredRoom);
        return Result.success(accroomService.update(updateWrapper));
    }

    // 添加聊天记录
    @PostMapping("/insertChat")
    public Object insertChat(@RequestBody Roomchitchat roomchitchat) {
        return Result.success(roomchitchatService.save(roomchitchat));
    }

    @RequestMapping(value = "/sengIMGANDSVG/{Type}", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public Object msgIMG(@RequestBody User user, @PathVariable(value = "Type", required = false) String Type) throws UnsupportedEncodingException {
        byte[] bytes = Base64Utils.decode(user.getUphoto().getBytes("UTF-8"));
        String uploadFile = UploadFile.uploadFile(bytes, UUID.randomUUID().toString() + "." + Type);
        return uploadFile;
    }

    // 房主删除房间
    @PutMapping("/deleteRoom/{rid}/{enteredRoom}")
    public Object deleteRoom(@PathVariable("rid") int rid, @PathVariable("enteredRoom") int enteredRoom) {
        // 房主删除房间
        LambdaUpdateWrapper<Room> roomLambdaUpdateWrapper = new LambdaUpdateWrapper<Room>();
        roomLambdaUpdateWrapper.set(Room::getIsDeleted, 1)
                .eq(Room::getRid, rid);
        boolean room = roomService.update(roomLambdaUpdateWrapper);

        // 房主退出房间
        LambdaUpdateWrapper<Accroom> updateWrapper = new LambdaUpdateWrapper<Accroom>();
        updateWrapper.set(Accroom::getIsDeleted, 1)
                .eq(Accroom::getRid, rid)
                .eq(Accroom::getEnteredRoom, enteredRoom);
        boolean accroom = accroomService.update(updateWrapper);

        if (room && accroom) {
            return Result.success(1);
        } else {
            return Result.success(0);
        }
    }


    //    用户创建房间
    @PostMapping("/createRoom")
    public Object createRoom(Room room) {
        roomService.save(room);
        int rID = roomService.last();
        Accroom accroom = new Accroom();
        accroom.setRid(rID);
        accroom.setRoomInitiator(Integer.valueOf(room.getRoomUID()));
        accroom.setEnteredRoom(Integer.valueOf(room.getRoomUID()));
        accroomService.save(accroom);
        return Result.success(roomService.AllShuJu(rID));
    }
}
